home *** CD-ROM | disk | FTP | other *** search
/ All for Cell Phones: Sony Ericsson / Sony-Ericsson 2004.iso / Java / Chess / Fly_Chess.jar / ChessGame.class (.txt) < prev    next >
Encoding:
Java Class File  |  2001-08-23  |  5.8 KB  |  260 lines

  1. import java.util.Random;
  2.  
  3. public class ChessGame {
  4.    private static final int POSTCOUNT_LIMIT = 25;
  5.    private int postcount = 0;
  6.    private boolean postactive = false;
  7.    private boolean show = false;
  8.    private boolean prevshow = false;
  9.    private boolean loggedon = false;
  10.    private boolean isplaying = false;
  11.    private boolean matchissued = false;
  12.    private boolean ischallenged = false;
  13.    private boolean drawoffered = false;
  14.    private String matchissue = "";
  15.    private String challenge = "";
  16.    private String matchreply = "";
  17.    private ChessLogic chesslogic = null;
  18.    private PostThread postthread = null;
  19.    private ServletResponseReceiver servletresponsereceiver = null;
  20.    private String status = "";
  21.  
  22.    public ChessGame(ChessLogic var1, String var2) {
  23.       this.chesslogic = var1;
  24.       Random var3 = new Random();
  25.       int var4 = var3.nextInt();
  26.       String var5 = CU.int2str(var4);
  27.       PostThread.setPostURL(var2);
  28.       PostThread.setSequenceNumber(0);
  29.       PostThread.setMIDletID(var5);
  30.    }
  31.  
  32.    private void adjustInternalState(String var1) {
  33.       StringTokenizer var2 = new StringTokenizer(var1);
  34.       if (var2.hasMoreElements()) {
  35.          var2.nextElement();
  36.       }
  37.  
  38.       String var3 = "|";
  39.       String var4 = "";
  40.       if (var2.hasMoreElements()) {
  41.          var3 = (String)var2.nextElement();
  42.       }
  43.  
  44.       while(!var3.equals("|")) {
  45.          var4 = var4 + var3 + " ";
  46.          if (var2.hasMoreElements()) {
  47.             var3 = (String)var2.nextElement();
  48.          } else {
  49.             var3 = "|";
  50.          }
  51.       }
  52.  
  53.       boolean var5 = false;
  54.  
  55.       while(!var5) {
  56.          this.parseMessage(var4);
  57.          this.show = false;
  58.          var4 = "";
  59.          if (var2.hasMoreElements()) {
  60.             var3 = (String)var2.nextElement();
  61.          }
  62.  
  63.          while(!var3.equals("|")) {
  64.             var4 = var4 + var3 + " ";
  65.             if (var2.hasMoreElements()) {
  66.                var3 = (String)var2.nextElement();
  67.             } else {
  68.                var3 = "|";
  69.             }
  70.          }
  71.  
  72.          if (var4.equals("") && !var2.hasMoreElements()) {
  73.             var5 = true;
  74.          }
  75.       }
  76.  
  77.    }
  78.  
  79.    public boolean doPost(String var1, boolean var2) {
  80.       if (this.postactive) {
  81.          return false;
  82.       } else {
  83.          ++this.postcount;
  84.          if (this.postcount > 25 && !CU.firstToken(var1).equals("logoff")) {
  85.             this.chesslogic.alert("This is a demo-version of Fly_Chess and limited to " + CU.int2str(25) + " posts to the NotTheFly servlet. \n" + "Please 'Logoff' before exit.\nContact NotTheFly for more information." + "End of Fly_Chess demo");
  86.             this.status = "End of Demo";
  87.             return false;
  88.          } else {
  89.             this.postactive = true;
  90.             this.prevshow = this.show;
  91.             this.show = var2;
  92.             if (this.postthread == null) {
  93.                this.postthread = new PostThread(this, var1);
  94.                this.postthread.start();
  95.                this.status = "Sending " + CU.firstToken(var1) + "-command...";
  96.                return true;
  97.             } else {
  98.                this.status = "Unable to send command";
  99.                return false;
  100.             }
  101.          }
  102.       }
  103.    }
  104.  
  105.    public boolean drawOffered() {
  106.       return this.drawoffered;
  107.    }
  108.  
  109.    public String getStatus() {
  110.       return this.status;
  111.    }
  112.  
  113.    public boolean isChallenged() {
  114.       return this.ischallenged;
  115.    }
  116.  
  117.    public boolean isPlaying() {
  118.       return this.isplaying;
  119.    }
  120.  
  121.    public boolean matchIssued() {
  122.       return this.matchissued;
  123.    }
  124.  
  125.    private void parseMessage(String var1) {
  126.       if (CU.containsExpr(var1, "<12>")) {
  127.          this.isplaying = true;
  128.          this.chesslogic.receiveServletResponse(var1);
  129.       } else if (CU.containsExpr(var1, "Style 12 set")) {
  130.          this.loggedon = true;
  131.       } else if (CU.containsExpr(var1, "Issuing:")) {
  132.          this.matchissued = true;
  133.          this.matchissue = var1;
  134.          this.chesslogic.alert(var1);
  135.       } else if (CU.containsExpr(var1, "Match request does not fit formula")) {
  136.          this.matchissued = false;
  137.          this.chesslogic.alert(var1);
  138.       } else if (CU.containsExpr(var1, "Challenge:")) {
  139.          this.ischallenged = true;
  140.          this.challenge = var1;
  141.          if (!this.isplaying) {
  142.             this.chesslogic.alert(var1);
  143.          }
  144.       } else if (CU.containsExpr(var1, "accepts your challenge")) {
  145.          this.isplaying = true;
  146.          this.matchissued = false;
  147.          this.chesslogic.alert(var1);
  148.          this.chesslogic.getChessGame().doPost("refresh", false);
  149.       } else if (CU.containsExpr(var1, "declines the match offer")) {
  150.          this.matchissued = false;
  151.          this.chesslogic.alert(var1);
  152.       } else if (CU.containsExpr(var1, "You accept the challenge")) {
  153.          this.isplaying = true;
  154.          this.ischallenged = false;
  155.       } else if (CU.containsExpr(var1, "You decline the match offer")) {
  156.          this.isplaying = false;
  157.          this.ischallenged = false;
  158.       } else if (!CU.containsExpr(var1, "You are not playing a game") && !CU.containsExpr(var1, "You are neither playing, observing nor examining a game")) {
  159.          if (!CU.containsExpr(var1, "Illegal move") && !CU.containsExpr(var1, "Not a legal move") && !CU.containsExpr(var1, "Your king is in check") && !CU.containsExpr(var1, "It isnΓÇÖt your turn") && !CU.containsExpr(var1, "It is not your move")) {
  160.             if (CU.containsExpr(var1, "offers you a draw")) {
  161.                this.drawoffered = true;
  162.                this.chesslogic.alert(var1);
  163.             } else if (CU.containsExpr(var1, "would like to abort")) {
  164.                this.chesslogic.alert(var1);
  165.             } else if (CU.containsExpr(var1, "{Game")) {
  166.                this.chesslogic.reset();
  167.                this.chesslogic.alert(var1);
  168.                this.isplaying = false;
  169.                this.drawoffered = false;
  170.                this.matchissued = false;
  171.             } else if (CU.containsExpr(var1, "telnet-failure")) {
  172.                this.chesslogic.alert("telnet-failure, in case of multiple failures, please logoff and try logon again");
  173.             } else if (CU.containsExpr(var1, "Maximum number of refreshes exceeded")) {
  174.                this.chesslogic.alert("Could not obtain new board situation, please choose 'nextstate' in MENU to try again");
  175.             } else if (this.postactive) {
  176.                if (this.prevshow) {
  177.                   this.chesslogic.alert(var1);
  178.                }
  179.             } else if (this.show) {
  180.                this.chesslogic.alert(var1);
  181.             }
  182.          } else {
  183.             this.chesslogic.alert(var1);
  184.          }
  185.       } else {
  186.          this.isplaying = false;
  187.          this.chesslogic.alert(var1);
  188.       }
  189.  
  190.    }
  191.  
  192.    public boolean postActive() {
  193.       return this.postactive;
  194.    }
  195.  
  196.    public synchronized void receiveServletResponse(String var1) {
  197.       this.postthread = null;
  198.       this.postactive = false;
  199.       this.status = "Response received...";
  200.       StringTokenizer var2 = new StringTokenizer(var1);
  201.       String var3 = "|";
  202.       String var4 = "";
  203.       if (var2.hasMoreElements()) {
  204.          var3 = (String)var2.nextElement();
  205.       }
  206.  
  207.       while(!var3.equals("|")) {
  208.          var4 = var4 + var3 + " ";
  209.          if (var2.hasMoreElements()) {
  210.             var3 = (String)var2.nextElement();
  211.          } else {
  212.             var3 = "|";
  213.          }
  214.       }
  215.  
  216.       this.servletresponsereceiver.receiveServletResponse(var4);
  217.       this.adjustInternalState(var1);
  218.       if (this.chesslogic.getCommandsForm().isActive() && !CU.firstToken(var1).equals("who")) {
  219.          this.chesslogic.getCommandsForm().makeCurrent(CU.getDisplay());
  220.       }
  221.  
  222.    }
  223.  
  224.    public void setServletRedirection(ServletResponseReceiver var1) {
  225.       this.servletresponsereceiver = var1;
  226.    }
  227.  
  228.    public boolean stopPost() {
  229.       this.status = "";
  230.       if (this.postthread != null) {
  231.          CU.shout("closing thread");
  232.  
  233.          try {
  234.             this.postthread.join();
  235.          } catch (Exception var2) {
  236.             CU.shout(((Throwable)var2).getMessage());
  237.             return false;
  238.          }
  239.  
  240.          CU.shout("thread closed");
  241.          this.postthread = null;
  242.          return true;
  243.       } else {
  244.          return true;
  245.       }
  246.    }
  247.  
  248.    public String viewChallenge() {
  249.       return this.challenge;
  250.    }
  251.  
  252.    public String viewMatchIssue() {
  253.       return this.matchissue;
  254.    }
  255.  
  256.    public String viewMatchReply() {
  257.       return this.matchreply;
  258.    }
  259. }
  260.